ScalaCheck: Property-based testing for Scala
ScalaCheck is a library written in Scala and used for automated property-based testing of Scala or Java programs. ScalaCheck was originally inspired by the Haskell library QuickCheck, but has also ventured into its own.
ScalaCheck has no external dependencies other than the Scala runtime, and works great with sbt, the Scala build tool. It is also fully integrated in the test frameworks ScalaTest, specs2 and LambdaTest. You can also use ScalaCheck completely standalone, with its built-in test runner.
ScalaCheck is used by several prominent Scala projects, for example the Scala compiler and the Akka concurrency framework.
News
2021-05-14: Scala 3.0 artifacts were published for ScalaCheck 1.15.4 and 1.15.3
2021-05-03: ScalaCheck 1.15.4 is released with a few small bug fixes
2021-02-16: ScalaCheck 1.15.3 adds support for Scala Native 0.4.0
2020-12-17: ScalaCheck 1.15.2 fixes some uninentional API breakage of methods
someOf
,atLeastOne
andpick
inGen
.2020-11-03: ScalaCheck 1.15.1 is released. Fixes some uninentional API breakage of methods
someOf
,atLeastOne
andpick
inGen
.2020-10-30: ScalaCheck 1.15.0 introduces a few enhancements and some performance improvements to string and character generation.
2019-12-13: ScalaCheck 1.14.3 fixed a defect with number generators. This also produces artifacts for Scala.js 1.0.0-RC2 and 0.6.31. See the change log.
2019-09-25: ScalaCheck 1.14.2 fixed a major defect with Scala.js but no other changes from 1.14.1.
2019-09-18: ScalaCheck 1.14.1 released! This is the first release since the ScalaCheck repository was moved to the Typelevel organisation. See the release notes.
2018-04-22: ScalaCheck 1.14.0! This release introduces support for deterministic testing. See release notes and change log.
2016-11-01: ScalaCheck 1.13.4 and ScalaCheck 1.12.6 released! These releases fix a deadlock problem with Scala 2.12.0. Also, as a problem was discovered with the previously released ScalaCheck 1.12.5 build, it is recommended that you upgrade to 1.12.6 or 1.13.4 even if you’re not using Scala 2.12.
2016-10-19: ScalaCheck 1.13.3 released! See release notes.
2016-07-11: ScalaCheck 1.13.2 released! See release notes.
2016-04-14: ScalaCheck 1.13.1 released! See release notes.
2016-02-03: ScalaCheck 1.13.0 released! See release notes.
2015-09-10: Snapshot builds are no longer published on this site. Instead Travis automatically publishes all successful builds of the master branch. See documentation for more information.
Quick start
Specify some of the methods of java.lang.String
like this:
import org.scalacheck.Properties
import org.scalacheck.Prop.forAll
object StringSpecification extends Properties("String") {
property("startsWith") = forAll { (a: String, b: String) =>
(a+b).startsWith(a)
}
property("concatenate") = forAll { (a: String, b: String) =>
(a+b).length > a.length && (a+b).length > b.length
}
property("substring") = forAll { (a: String, b: String, c: String) =>
(a+b+c).substring(a.length, a.length+b.length) == b
}
}
If you use sbt add the following dependency to your build file:
+= "org.scalacheck" %% "scalacheck" % "1.14.1" % "test" libraryDependencies
Put your ScalaCheck properties in src/test/scala
, then use the test
task to check them:
$ sbt test
+ String.startsWith: OK, passed 100 tests.
! String.concat: Falsified after 0 passed tests.
> ARG_0: ""
> ARG_1: ""
+ String.substring: OK, passed 100 tests.
As you can see, the second property was not quite right. ScalaCheck discovers this and presents the arguments that make the property fail, two empty strings. The other two properties both pass 100 test rounds, each with a randomized set of input parameters.
You can also use ScalaCheck standalone, since it has a built-in command line test runner. Compile and run like this:
$ scalac -cp scalacheck_2.11-1.14.1.jar StringSpecification.scala
$ scala -cp .:scalacheck_2.11-1.14.1.jar StringSpecification